package com.sxbbc.plat.interceptor;

import com.sxbbc.common.basics.exceptions.ThrowJsonException;
import com.sxbbc.common.basics.exceptions.ThrowPageException;
import com.sxbbc.common.core.entity.jz.SysUser;
import com.sxbbc.plat.common.*;
import com.sxbbc.plat.util.PlatSession;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author leehao
 * @description 登录拦截器
 * @date 2019-04-26 09:11
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        response.setCharacterEncoding("utf-8");
        SysUser sysUser = PlatSession.getSysUser(request);
        String type = request.getHeader("X-Requested-With");
        if (sysUser == null) {
            if ("XMLHttpRequest".equalsIgnoreCase(type)) {
                response.setHeader("sessionstatus", "timeout");
            } else {
                response.sendRedirect(MappingUtils.SHOW_LOGIN);
            }
            return false;
        } else {
            String returnUrl = String.valueOf(request.getServletPath());
            List<String> strs = PlatSession.getMenuUrl(request);
            strs.add(MappingUtils.SHOW_INDEX);
            strs.add(MappingUtils.SHOW_WELCOME);
            strs.add(MappingUtils.SHOW_UPDATE_PASSWORD);
            strs.add(MappingUtils.UPDATE_PASSWORD);
            strs.add(MappingUtils.SHOW_ROLE_EDIT);
            strs.add(CustomerMapping.SHOW_EDIT_CUSTOMER_LEVEL);
            strs.add(ProductMapping.QUERY_CLASSIFY_LIST_AJAX);
            strs.add(ProductMapping.SHOW_EDIT_CLASSFIY);
            strs.add(MappingUtils.QUERY_ADDRESS_NOPAGE);
            strs.add(MappingUtils.SHOW_NEWS_EDIT);
            strs.add(OrderMapping.SHOW_ORDER_DETAIL);
            strs.add(ProductMapping.QUERY_INDUSTRY_DETAIL_BY_ID);
            strs.add(MappingUtils.UPLOAD_RICH_FILE);
            strs.add(CustomerMapping.SELECT_CUSTOMER_EXIST);
            strs.add(MappingUtils.QUERY_ADDRESS_LIST);
            strs.add(MappingUtils.QUERY_LOGS_LIST);
            strs.add(CustomerMapping.QUERY_CUSTOMER_AUTHEN);
            strs.add(OrderMapping.QUERY_ORDER_LIST);
            strs.add(OrderMapping.QUERY_REFUND_LIST);
            strs.add(AgentMapping.HANDLE_UPDATE_STATUS);
            strs.add(StatisMapping.QUERY_CUSTOMER_STATIS_DIFFLINE);
            strs.add(StatisMapping.QUERY_CUSTOMER_STATIS_PIE);
            strs.add(StatisMapping.QUERY_ACCOUNT_STATIS);
            strs.add(AccountMapping.QUERY_PLAT_DEAL_DATA);
            strs.add(MappingUtils.QUERY_ESSAY_LIST);
            strs.add(MappingUtils.QUERY_NEWS_LIST);
            strs.add(MappingUtils.QUERY_OPINION_LIST);
            strs.add(AgentMapping.QUERY_AGENT_LIST);
            strs.add(CustomerMapping.QUERY_CUSTOMER_LIST);
            strs.add(StatisMapping.QUERY_REFUND_MONEY_STATIS);
            strs.add(StatisMapping.QUERY_REFUND_NUM_STATIS);
            strs.add(StatisMapping.QUERY_PRO_SALE_STATIS);
            strs.add(AgentMapping.QUERY_ACCOUNT_DETAIL);
            strs.add(CustomerMapping.GET_CUSTOMER);
            strs.add(ProductMapping.QUERY_PRODUCT);
            strs.add(AccountMapping.QUERY_BUSINESS_ACCOUNT_DETAIL);
            strs.add(StatisMapping.QUERY_ORDER_NUM_STATIS);
            strs.add(AccountMapping.QUERY_BUSINESS_ORDER_REFUND_DETAIL);
            strs.add(AccountMapping.QUERY_BUSINESS_ORDER_REFUND_DETAIL);
            strs.add(AgentMapping.SHOW_EDIT_AGENT);
            strs.add(AgentMapping.SHOW_ACCOUNT_DETAIL);
            strs.add(CustomerMapping.SHOW_CUSTOMER_POINT_LIST);
            strs.add(CustomerMapping.SHOW_EDIRECTORINDIRECT_CUSTOMER);
            strs.add(CustomerMapping.SHOW_CUSTOMER_POINT_LIST);
            strs.add(CustomerMapping.SHOW_CUSTOMER_LEVEL_ADJUST);
            strs.add(MappingUtils.SELECT_ROLE_LIST);
            strs.add(CustomerMapping.UPDATE_PARENT);
            strs.add(CustomerMapping.SHOW_CHANGE_POINT);
            strs.add(CustomerMapping.HANDLE_CHANGE_POINT);
            strs.add(CustomerMapping.SHOW_CUSTOMER_EDIT_INFO);
            strs.add(CustomerMapping.HANDLE_CUSTOMER_EDIT);
            strs.add(ChickenMapping.HANDLE_CHICKEN_BONUS_DAY_EDIT);
            strs.add(AgentMapping.EDIT_AGENT);
            strs.add(CustomerMapping.SELECT_MY_TEAM);
            strs.add(CustomerMapping.SHOW_MY_TEAM);
            boolean hasPermission = strs.contains(returnUrl);
            if (StringUtils.equalsIgnoreCase("XMLHttpRequest", type)) {
                if (!hasPermission) {
                    throw new ThrowJsonException("您还没有此功能权限不能操作");
                }
            } else {
                if (!hasPermission) {
                    throw new ThrowPageException("您还没有此功能权限不能操作");
                }
            }
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
